﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;


namespace DataExportImporter
{
    public class Export
    {
        public string Username;
        public string Password;
        public string DomainName;
        public string WebPath;
        public string SubSite;
        public string ListName;

        public Export()
        {

           

        }

        


        public System.Xml.XmlNode GetListData(string queryXml)
        {

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(queryXml);



            System.Xml.XmlNode listQuery = doc.SelectSingleNode("//Query");
            System.Xml.XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");
            System.Xml.XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions");
            System.Xml.XmlNode ServerDetails = doc.SelectSingleNode("//Server");



             Username = XmlHelper.GetValue(ServerDetails, "Username");
             Password = XmlHelper.GetValue(ServerDetails, "Password");
             DomainName = XmlHelper.GetValue(ServerDetails, "DomainName");
             WebPath = XmlHelper.GetValue(ServerDetails, "Url");
             SubSite = XmlHelper.GetValue(ServerDetails, "SubSite");
             ListName = XmlHelper.GetValue(ServerDetails, "ListName");






            listsWebService.Lists list = new DataExportImporter.listsWebService.Lists();
            list.Url = WebPath + SubSite + "/_vti_bin/lists.asmx";

            list.UseDefaultCredentials = false;
            list.Credentials = new NetworkCredential(Username, Password, DomainName);


            Guid g = GetWebID(WebPath);

           

            System.Xml.XmlNode items = list.GetListItems(ListName, string.Empty, listQuery, listViewFields, "1000", listQueryOptions, g.ToString());

            return items;
        }



        private Guid GetWebID(string webPath)
        {

            SiteDataWebService.SiteData siteDataWS = new SiteDataWebService.SiteData();

            siteDataWS.UseDefaultCredentials = false;
            siteDataWS.Credentials = new NetworkCredential(Username, Password, DomainName);


            SiteDataWebService._sWebMetadata webMetaData;

            SiteDataWebService._sWebWithTime[] arrWebWithTime;

            SiteDataWebService._sListWithTime[] arrListWithTime;

            SiteDataWebService._sFPUrl[] arrUrls;

            string roles; string[] roleUsers; string[] roleGroups;

            siteDataWS.Url = webPath + SubSite + "/_vti_bin/sitedata.asmx";

            uint i = siteDataWS.GetWeb(out webMetaData, out arrWebWithTime, out arrListWithTime, out arrUrls, out roles, out roleUsers, out roleGroups);

            Guid g = new Guid(webMetaData.WebID);

            return g;

        }

    }
}
